<?php
/**
 * menu.php
 * @author ranuandra
 *
 * 9 Agu 2011 08:36:19
 *
 * 
 */

class Controller_Menu extends Kohana_Mapi
{

	public function action_index()
	{
		$menu = ORM::factory('menu')->find_all();
		$container = View::factory('menu/index');
		$container->title = "Daftar Menu";
		$container->datas = $menu;
		if(Request::current()->is_ajax() == $this->request)
		{	
			$container->ajax = true;
			echo $container;
			exit;
		}
		else 
		{	$container->ajax = false;
			$menu = View::factory('module/menu');
			$this->template->menu = $menu;
			$this->template->container = $container;
		}
	}
	
	public function action_edit($id)
	{
		$form = View::factory('menu/form');
		$form->form = ORM::factory('menu')->get_form();
		$form->title = "Edit Data Menu";
		if(Request::current()->is_ajax() == $this->request)
		{
			echo $form;
		}
		else 
		{
			$this->template->container = $form;
		}
	}
	
	public function action_save()
	{
		if(isset($_POST))
		{

			try {
				//$post = Validation::factory($_POST);
					$menu = ORM::factory('menu');
					$menu->create_menu($_POST);
					//Request::current()->redirect(URL::base().'customer');
					if(Request::current()->is_ajax() == $this->request)
					{
						$auto_render = FALSE;
						echo "sukses";
						exit;
					}
					else 
					{
						Request::current()->redirect(URL::base().'menu');
					}

				}
				catch(ORM_Validation_Exception $e)
				{
					$errors = $e->errors('models');
					print_r($errors);
				}
			
		}
		else
		{
			Request::current()->redirect(URL::base().'menu/edit');	
		}	
	}
}